package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime begin,LocalDateTime end) {

        //将时间存储在map集合中
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);

        //查询总订单数
        Integer totalOrderCount = orderMapper.getDailyOrderCount(map);

        map.put("status", Orders.COMPLETED);

        //查询今天营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover==null?0.0:turnover;

        //有效订单数
        Integer dailyValidOrderCount = orderMapper.getDailyValidOrderCount(map);

        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if (totalOrderCount != 0 && dailyValidOrderCount != 0) {
            //订单完成率
            orderCompletionRate = dailyValidOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / dailyValidOrderCount;
        }

        //新增用户
        Integer newUsers = userMapper.countByMap(map);

        //创建BusinessDataVO对象并返回
        return BusinessDataVO.builder()
                .turnover(turnover)
                .orderCompletionRate(orderCompletionRate)
                .newUsers(newUsers)
                .validOrderCount(dailyValidOrderCount)
                .unitPrice(unitPrice)
                .build();

    }

    /**
     * 工作台订单管理
     *
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        //获取当天时间
        //开始时间
        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
        Map map = new HashMap();
        map.put("begin", begin);
        //全部订单
        Integer allOrders = orderMapper.countByMap(map);

        //待接单
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);
        //待派送数量
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);

        //已完成数量
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);

        //已取消数量
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();

    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        //查询已起售菜品总量
        Integer sold = dishMapper.countByMap(map);

        // 已停售数量
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        //查询已起售套餐总量
        Integer sold = setmealMapper.countByMap(map);

        // 已停售数量
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
